package com.apache.passport.common;

import com.apache.tools.ConfigUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

/**
 * description:  加密工具类（desSec）
 *
 * @author Hou Dayu 创建时间：2016-11-27
 */
public class DesUtils {

    private String strDefaultKey = "";

    private static DesUtils instance = null;

    public static DesUtils getInstance() {
        if (instance == null) {
            instance = new DesUtils();
        }
        return instance;
    }

    private DesUtils() {
        strDefaultKey = ConfigUtil.getInstance().getValueByKey("desSecKey");
    }

    public byte[] desEncrypt(byte[] plainText) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(strDefaultKey.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        byte data[] = plainText;
        byte encryptedData[] = cipher.doFinal(data);
        return encryptedData;
    }

    public byte[] desDecrypt(byte[] encryptText) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec desKeySpec = new DESKeySpec(strDefaultKey.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key, sr);
        byte encryptedData[] = encryptText;
        byte decryptedData[] = cipher.doFinal(encryptedData);
        return decryptedData;
    }

    public String encrypt(String input) {
        String result = "";
        try {
            result = base64Encode(desEncrypt(input.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String decrypt(String input) {
        String result = "";
        try {
            byte[] results = base64Decode(input);
            result = new String(desDecrypt(results));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String base64Encode(byte[] s) {
        if (s == null)
            return null;
        BASE64Encoder b = new BASE64Encoder();
        return b.encode(s);
    }

    public static byte[] base64Decode(String s) throws IOException {
        if (s == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(s);
        return b;
    }

    public static void main(String[] args) {
        try {
            String test = "test1028";
            //			DesUtils des = new DesUtils();
            //			//			25f9fb655120267f72786d42de8ba6c1
            //			System.out.println("加密前的字符：" + test);
            //			System.out.println("加密后的字符：" + des.encrypt(test, "iussoft"));
            //			System.out.println("解密后的字符：" + des.decrypt(des.encrypt(test, "iussoft")));

            String code = DesUtils.getInstance().encrypt(test);
            System.out.println("加密后的字符：" + code);
            code = DesUtils.getInstance().decrypt("TMR29YtnGPI=");
            System.out.println("解密后的字符：" + code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
